package com.controller;
import com.entity.*;
import com.service.InsuranceService;
import com.service.InvestigationService;
import com.service.OrdersService;
import com.utils.MapUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.InitializingBean;
import com.service.ResponsibilityService;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import com.utils.ResultUtil;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Date;
import com.github.pagehelper.PageHelper;
import org.springframework.web.bind.annotation.RequestParam;
import com.github.pagehelper.PageInfo;
@Controller
@RequestMapping(value="/admin/responsibility")
public class AdminResponsibilityController extends BaseController {

    @Autowired
    private ResponsibilityService responsibilityService;
    @Autowired
    private InvestigationService investigationService;
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private InsuranceService insuranceService;
   
  

     @RequestMapping("add")
    public String add(HttpServletRequest request,Responsibility model){
        
        request.setAttribute("model",model);
        return "/admin/responsibility-add";
    }
  
  
     @RequestMapping("edit")
    public String edit(HttpServletRequest request,String id){
       Responsibility model = responsibilityService.getModel(id);
       request.setAttribute("model",model);
        return "/admin/responsibility-add";
    }

    @RequestMapping("kancha")
    public String kancha(HttpServletRequest request,String id){
        Orders model = ordersService.getModel(id);
        Insurance insurance = insuranceService.getModel(model.getInsuranceId());
        request.setAttribute("model",model);
        request.setAttribute("insurance",insurance);
        Responsibility responsibility = responsibilityService.selectOneModel(MapUtil.buildMap("ordersId", model.getId()));
        if (responsibility != null){
            request.setAttribute("responsibility",responsibility);
            return "/admin/responsibility-view";
        }
        return "/admin/responsibility-add";
    }
  
    @RequestMapping("save")
    @ResponseBody
    public Result save(HttpServletRequest request,Responsibility model){
        User user = (User) request.getSession().getAttribute("user");
        int i = 0;
        if (StringUtils.isBlank(model.getId())) {
            model.preInsert();
            model.setCreateDate(new Date());
            i = responsibilityService.insertModel(model);
            //更新保单状态
            Orders orders = ordersService.getModel(model.getOrdersId());
            orders.setState(3);
            ordersService.updateModel(orders);
        } else {
            i = responsibilityService.updateModel(model);
        }
        if (i == 0) {
            return ResultUtil.error("失败");
        } else {
            return ResultUtil.success(null);
        }


    }
  
  
  //删除批量删除
    @RequestMapping("delete")
    @ResponseBody
    public Result delete(HttpServletRequest request,String id){
        
         if (StringUtils.isNotBlank(id)){
            String[] split = id.split(",");
            for (int i = 0; i < split.length; i++) {
                responsibilityService.deleteModel(split[i]);
            }
            return ResultUtil.success();
        }else {
            return ResultUtil.error("未选中删除项！");
        }
        
    }
  
  
   @RequestMapping("list")
    public String list(HttpServletRequest request,Responsibility model){
       
        request.setAttribute("model",model);
        return "/admin/responsibility-list";
    }
  
    //获取列表数据
    @RequestMapping("list/data")
    @ResponseBody
    public Result list(HttpServletRequest request,User model, @RequestParam(defaultValue = "1")Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        List<Responsibility> list = responsibilityService.getModelList(putEntityInMap(model));
        PageInfo<Responsibility> pageInfo = new PageInfo<>(list);
        return  ResultUtil.success(list,pageInfo.getTotal());
    }
   

}